package com.example.demo.leetcode;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class _295_数据流的中位数 {
    public static void main(String[] args) {
        MedianFinder medianFinder = new MedianFinder();
        medianFinder.addNum(6);
        double median = medianFinder.findMedian();
        System.out.println(median);

        medianFinder.addNum(10);
        double median1 = medianFinder.findMedian();
        System.out.println(median1);

        medianFinder.addNum(2);
        double median2 = medianFinder.findMedian();
        System.out.println(median2);

        medianFinder.addNum(6);
        double median3 = medianFinder.findMedian();
        System.out.println(median3);

        medianFinder.addNum(5);
        double median4 = medianFinder.findMedian();
        System.out.println(median4);



    }
}


class MedianFinder {

    List<Integer> list = new ArrayList<>();

    public MedianFinder() {

    }

    public void addNum(int num) {
        list.add(num);
        Collections.sort(list);
    }

    public double findMedian() {
        int size = list.size();
        int i = size % 2;
        double res;
        if (i == 0) {
            res = (list.get(size / 2) + list.get(size / 2 - 1)) / 2.0;
        } else {
            res = list.get(size / 2);
        }
        return res;
    }
}
